//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Query Application Block
//===============================================================================

using System;
using System.Reflection;
using System.ComponentModel;
using System.Drawing.Design;
using System.Web.Services.Protocols;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design.Validation;
using EntLibContrib.Common.Configuration.Design;
using EntLibContrib.Common.Configuration.Design.Editors;
using EntLibContrib.Query.Properties;

namespace EntLibContrib.Query.Configuration.Design
{
	/// <summary>
	/// Represents a query using a Web Service.
	/// </summary>
	public sealed class WebServiceQueryNode : ServiceQueryNode
	{
		#region Properties
		/// <summary>
		/// Gets or sets the name of the service.
		/// </summary>
		/// <value>The service name for this node.</value>
		[Required]
		[Editor(typeof(MethodSelectorEditor), typeof(UITypeEditor))]
		[BaseType(typeof(WebClientProtocol), TypeSelectorIncludes.None)]
		[MethodFilter(BindingFlags.DeclaredOnly|BindingFlags.Instance|BindingFlags.Public)]
		public override TypeMethodName TypeMethodName
		{
			get
			{
				return base.TypeMethodName;
			}
			set
			{
				base.TypeMethodName = value;
			}
		}
		/// <summary>
		/// Gets a <see cref="WebServiceQueryData"/> configuration object using the node data.
		/// </summary>
		/// <value>
		/// A <see cref="WebServiceQueryData"/> configuration object using the node data.
		/// </value>
		[ReadOnly(true)]
		public override QueryDataBase QueryData
		{
			get { return new WebServiceQueryData(Name, CustomCommandName, ParameterSetName, Address, TypeMethodName.TypeName, TypeMethodName.MethodName); }
		}
		#endregion

		#region Construction
		/// <summary>
		/// Initialize a new instance of the <see cref="WebServiceQueryNode"/> class.
		/// </summary>
		public WebServiceQueryNode ()
			: this(new WebServiceQueryData(InternalResources.DefaultWebServiceQueryNodeName, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty))
		{
		}

		/// <summary>
		/// Initialize a new instance of the<see cref="WebServiceQueryNode"/> class with a <see cref="WebServiceQueryData"/> configuration object.
		/// </summary>
		/// <param name="webServiceQueryData">The webService query data.</param>
		public WebServiceQueryNode (WebServiceQueryData webServiceQueryData)
		{
			if ( webServiceQueryData == null )
				throw new ArgumentNullException("webServiceQueryData");

			Rename(webServiceQueryData.Name);
			base.CustomCommandName = webServiceQueryData.CustomCommandName;
			base.ParameterSetName = webServiceQueryData.ParameterSetName;
			base.Address = webServiceQueryData.Address;
			base.TypeMethodName = new TypeMethodName(webServiceQueryData.Service, webServiceQueryData.Method);
		}
		#endregion
	}
}